import { Injectable } from '@angular/core';
import {HttpClient} from '@angular/common/http';
import {Category} from '../../../domain/Category';
import {Response} from '../../../domain/Response';
import {Observable} from 'rxjs';
import {LOADING_MESSAGE} from '../localstorage/cache.service.namespace';
import {CacheService} from '../localstorage/cache.service';

@Injectable({
  providedIn: 'root'
})
export class CateApiService {

  constructor(private http: HttpClient, private cache: CacheService) { }

  public categories(): Observable<Response<Category[]>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin/categories', message:'获取数据中....'});
    return this.http.get<Response<Category[]>>('/admin/categories')
  }

  public create(cate: Category): Observable<Response<Category>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin/category', message:'创建分类中....'});
    return this.http.post<Response<Category>>('/admin/category',cate);
  }

  public delete(id:number):Observable<Response<any>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin//category/'+id, message:'删除分类中....'});
    return this.http.delete<Response<any>>('/admin/category/'+id);
  }

  public update(cate: Category): Observable<Response<Category>> {
    this.cache.set(LOADING_MESSAGE,{url:'/admin/category'+cate.id, message:'更新分类中....'});
    return this.http.put<Response<Category>>('/admin/category',cate);
  }
}
